﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Windows.Controls.Primitives;

namespace _3.Students
{
    public partial class MainPage : UserControl
    {
        private ObservableCollection<Student> students;
        private Popup popupConfirm;

        public MainPage()
        {
            InitializeComponent();
            students = new ObservableCollection<Student>();
        }

        private void ButtonAdd_Click(object sender, RoutedEventArgs e)
        {
            if (IsDataCorrect())
            {
                Student newStudent = new Student();
                newStudent.FirstName = TextBoxFirstName.Text;
                newStudent.LastName = TextBoxLastName.Text;
                newStudent.FacNumber = int.Parse(TextBoxFacNumber.Text);
                newStudent.Mark = float.Parse(TextBoxMark.Text);

                students.Add(newStudent);

                DataGridStudents.DataContext = students;
                CalculateAverages();
                MessageBox.Show("Student added!");                
                //ButtonClear_Click(null, e);
                //double top = (double)ButtonDelete.GetValue(Canvas.TopProperty);
                //MessageBox.Show(DataGridStudents.GetValue(Grid.HeightProperty).ToString());
                //ButtonDelete.SetValue(Canvas.TopProperty, top + 24);                
            }            
        }

        private bool IsDataCorrect()
        {
            if (TextBoxFirstName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Enter first name!");
                TextBoxFirstName.Focus();
                return false;
            }
            if (TextBoxLastName.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Enter last name!");
                TextBoxLastName.Focus();
                return false;
            }
            int facNumber = 0;
            if (!int.TryParse(TextBoxFacNumber.Text, out facNumber))
            {
                MessageBox.Show("Enter correct faculty number!");
                TextBoxFacNumber.Focus();
                return false;
            }
            float mark = 0f;
            if (!float.TryParse(TextBoxMark.Text, out mark))
            {
                MessageBox.Show("Enter mark between 2 and 6!");
                TextBoxMark.Focus();
                return false;
            }
            else
            {
                if (!(mark >= 2f && mark <= 6f))
                {
                    MessageBox.Show("Enter mark between 2 and 6!");
                    TextBoxMark.Focus();
                    return false;
                }
            }
            return true;
        }

        private void CalculateAverages()
        {
            float min = float.MaxValue;
            float max = float.MinValue;
            float average = 0f;
            int number = students.Count;
            foreach (Student student in students)
            {
                if (student.Mark < min)
                {
                    min = student.Mark;
                }
                if (student.Mark > max)
                {
                    max = student.Mark;
                }
                average += student.Mark;
            }
            average /= number;
            TextBoxMinimum.Text = min.ToString();
            TextBoxMaximum.Text = max.ToString();
            TextBoxAverage.Text = average.ToString();
            TextBoxNumberStudents.Text = number.ToString();
        }

        private void ButtonClear_Click(object sender, RoutedEventArgs e)
        {

            TextBoxFirstName.Text = string.Empty;
            TextBoxLastName.Text = string.Empty;
            TextBoxFacNumber.Text = string.Empty;
            TextBoxMark.Text = string.Empty;
        }

        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (DataGridStudents.SelectedItems.Count > 0)
            {
                popupConfirm = new Popup();
                Button buttonPopupCancel = new Button();
                buttonPopupCancel.Content = "Cancel";
                buttonPopupCancel.Height = 20d;
                buttonPopupCancel.Click += new RoutedEventHandler(ButtonPopupCancel_Click);

                Button buttonPopupOk = new Button();
                buttonPopupOk.Content = "OK";
                buttonPopupOk.Height = 20d;
                buttonPopupOk.Click += new RoutedEventHandler(ButtonPopupOk_Click);

                TextBlock textBlock = new TextBlock();
                textBlock.Text = "Are you sure?!";
                
                StackPanel stackPanelPopupButtons = new StackPanel();
                stackPanelPopupButtons.Orientation = Orientation.Horizontal;
                stackPanelPopupButtons.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
                stackPanelPopupButtons.Children.Add(buttonPopupOk);
                stackPanelPopupButtons.Children.Add(buttonPopupCancel);

                StackPanel stackPanelPopupMain = new StackPanel();
                stackPanelPopupMain.Background = new SolidColorBrush(Colors.LightGray);
                stackPanelPopupMain.Children.Add(textBlock);
                stackPanelPopupMain.Children.Add(stackPanelPopupButtons);

                Border border = new Border();
                border.BorderBrush = new SolidColorBrush(Colors.Red);
                border.BorderThickness = new Thickness(1d);

                border.Child = stackPanelPopupMain;
                popupConfirm.Child = border;
                
                popupConfirm.VerticalOffset = 100;
                popupConfirm.HorizontalOffset = 250;
                popupConfirm.IsOpen = true;
            }
            else
            {
                MessageBox.Show("Select item to delete!");
            }
        }

        private void ButtonPopupOk_Click(object sender, RoutedEventArgs e)
        {
            students.Remove(DataGridStudents.SelectedItem as Student);
            popupConfirm.IsOpen = false;
        }

        private void ButtonPopupCancel_Click(object sender, RoutedEventArgs e)
        {
            popupConfirm.IsOpen = false;
        }
    }
}
